Passed
Push — master ( 16592c...98d272 )
by Muhammad Dyas
01:36
created

utils.js ➔ extractConfig   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
1
/**
2
 * @param {array} names - list of names
3
 * @param {integer} winnerCount - total winner that picked from names
0 ignored issues
show
Documentation introduced by
The parameter winnerCount does not exist. Did you maybe forget to remove this comment?
Loading history...
4
 * @returns {array} - list of winner
5
 */
6
export function getRandomWinners(names, winnerCount = 1) {
7
  for (let i = names.length - 1; i > 0; i--) {
8
    const j = Math.floor(Math.random() * (i + 1));
9
    [names[i], names[j]] = [names[j], names[i]];
0 ignored issues
show
Bug introduced by
The variable names seems to be never declared. If this is a global, consider adding a /** global: names */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable j seems to be never declared. If this is a global, consider adding a /** global: j */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable i seems to be never declared. If this is a global, consider adding a /** global: i */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
  }
11
  return names.slice(0, winnerCount);
12
}
13
14
/**
15
 * @param {string} string - text that will be extracted
16
 * @returns {array} list of extracted text
17
 */
18
export function extractMessageByDoubleQuote(string) {
19
  const regex = /"[^"]+"/g;
20
  const extracted = string.match(regex);
21
  if (extracted) {
22
    return extracted.map((s) => s.replace(/"(.+)"/, '$1'));
23
  }
24
  return [];
25
}
26
27
/**
28
 * @param {string} string - text that will be extracted
29
 * @returns {array} list of extracted text
30
 */
31
export function extractMessageAndConfig(string) {
32
  const regex = /"[^"]+"|[^\s]+/g;
33
  return string.match(regex).map((s) => s.replace(/"(.+)"/, '$1'));
34
}
35
36
/**
37
 * Config means anything else beside string inside quote
38
 * @param {string} argumentText -
39
 * @param {array} messages - extracted messages
40
 * @returns {array} extracted command
41
 */
42
export function extractConfig(argumentText, messages) {
43
  const extractedTextCommands = extractMessageAndConfig(argumentText);
44
  return extractedTextCommands.filter((val) => !messages.includes(val));
45
}
46
47
/**
48
 * Randomly shuffles an array in place using the Fisher-Yates algorithm.
49
 *
50
 * @param {array} array - The array to be shuffled.
51
 * @returns {array} - The shuffled array.
52
 */
53
export function shuffle(array) {
54
  for (let i = array.length - 1; i > 0; i--) {
55
    const j = Math.floor(Math.random() * (i + 1));
56
    [array[i], array[j]] = [array[j], array[i]];
0 ignored issues
show
Bug introduced by
The variable j seems to be never declared. If this is a global, consider adding a /** global: j */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable i seems to be never declared. If this is a global, consider adding a /** global: i */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable array seems to be never declared. If this is a global, consider adding a /** global: array */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
57
  }
58
  return array;
59
}
60
61
62
/**
63
 * helper function to generate random numbers
64
 *
65
 * @param {number} start - the start of the range
66
 * @param {number} end - the end of the range
67
 * @returns {number[]} - an array of random numbers
68
 */
69
export function generateRandomNumbers(start, end) {
70
  return Array.from({length: end - start + 1}, (_, i) => i + start);
71
}
72